﻿using Microsoft.Extensions.Options;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

using WebApiDemo.Config;

using zijian666.WebApi;
using zijian666.WebApi.Attributes;

namespace WebApiDemo.Controllers
{
    public class TestController : ApiControllerBase
    {
        private readonly AliConfig _aliConfig;
        private readonly IHttpClientFactory httpClientFactory;

        public TestController(AliConfig aliConfig, IOptions<AliConfig> settings, IHttpClientFactory httpClientFactory)
        {
            _aliConfig = aliConfig;
            this.httpClientFactory = httpClientFactory;
        }
        [HttpApi]
        public string Hello(string name) => $"你好！{name}。";

        [HttpApi("demo")]
        public object Test() => _aliConfig;

        [HttpApi]
        public string Error(bool isSystem)
        {
            if (isSystem)
            {
                var i = 0;
                return (i / i).ToString();
            }
            throw new Exception("11111") { HResult = 1 };
        }

        [HttpApi]
        public object Fail()
        {
            return new Exception("请求失败") { HResult = 2 };
        }

        [HttpApi("BadRequest")]
        public object _BadRequest()
        {
            return BadRequest("请求失败");
        }

        [HttpApi]
        public async Task<object> HttpClient()
        {
            await httpClientFactory.CreateClient("").GetAsync("http://localhost:5000/api/test/hello");
            return "ok";
        }

    }
}
